package com.example.demo.service;

import com.example.demo.mapper.UserMapper;
import com.example.demo.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.NESTED)
    public int add(UserInfo userInfo){
        int result = userMapper.add(userInfo);
        System.out.println("add result -> " + result);
        return result;
    }

    @Transactional(propagation = Propagation.NESTED)
    public int insert(UserInfo userInfo){
        int result = userMapper.add(userInfo);
        System.out.println("insert result -> " + result);
        int num = 10 / 0;
//        try {
//            int num = 10 / 0;
//        }catch (Exception e){
//            // 手动回滚
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }

        return result;
    }


}
